import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/index")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletContext servletContext=getServletContext();
        Object count=servletContext.getAttribute("count");

        if(count==null){
            count=0;
            servletContext.setAttribute("count",count);
        }
        int number=(int)count+1;
        System.out.println("number:"+number);
        servletContext.setAttribute("count",number);

        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().println("您是第"+number+"位用户");
        resp.setHeader("refresh","3;url=http://localhost:8080/first-servlet/login.html");//设置3秒之后自动跳转

    }
}
